<?php
class MyDOMDocument extends DOMDocument {
    public function toArray(DOMNode $oDomNode = null){
        if (is_null($oDomNode) && !$this->hasChildNodes()) return array();
        $oDomNode = (is_null($oDomNode)) ? $this->documentElement : $oDomNode;
        if (!$oDomNode->hasChildNodes()) {
			$mResult = $oDomNode->nodeValue;
        } else {
            $mResult = array();
            foreach ($oDomNode->childNodes as $oChildNode) {
                $oChildNodeList = $oDomNode->getElementsByTagName($oChildNode->nodeName); 
                $iChildCount = 0;
                foreach ($oChildNodeList as $oNode) {
                    if ($oNode->parentNode->isSameNode($oChildNode->parentNode)) $iChildCount++;
                }
				if( trim($oChildNode->nodeValue)!=='' ){
					$mValue = $this->toArray($oChildNode);
					$sKey   = ($oChildNode->nodeName{0} == '#') ? 0 : $oChildNode->nodeName;
					$mValue = is_array($mValue) ? $mValue[$oChildNode->nodeName] : $mValue;
					if ($iChildCount > 1){
						$mResult[$sKey][] = $mValue;
					} else {
						$mResult[$sKey] = $mValue;
					}
				}
            }
            if (count($mResult) == 1 && isset($mResult[0]) && !is_array($mResult[0]))
                $mResult = $mResult[0];
        }
        $arAttributes = array();
        if ($oDomNode->hasAttributes()) {
            foreach ($oDomNode->attributes as $sAttrName=>$oAttrNode)
                $arAttributes["@{$oAttrNode->nodeName}"] = $oAttrNode->nodeValue;
        }
        if ($oDomNode instanceof DOMElement && $oDomNode->getAttribute('xmlns'))
            $arAttributes["@xmlns"] = $oDomNode->getAttribute('xmlns');
        if (count($arAttributes)) {
            if (!is_array($mResult)) $mResult = (trim($mResult)) ? array($mResult) : array();
            $mResult = array_merge($mResult, $arAttributes);
        }
        $arResult = array($oDomNode->nodeName=>$mResult);
        return $arResult;
    }
}
$dom = new MyDOMDocument();
$dom->load('test/xml/xmltest.xml');
print_r($dom->toArray());
